package br.com.br1.fv.mobile.activity;

import java.util.Collection;

import org.droidsql.DAO;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import br.com.br1.droidutils.MessageUtil;
import br.com.br1.fv.db.entities.Cliente;
import br.com.br1.fv.mobile.FlowControl;
import br.com.br1.fv.mobile.R;

public class ListaClientesActivity extends TabActivity implements TabContentFactory{

	private static final String ABA_SEQ_VISITAS = "ABA_SEQ_VISITAS";
	private static final String ABA_LISTA_CLIENTES = "ABA_LISTA_CLIENTES";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TabHost host = getTabHost();
		
		//LayoutInflater.from(this).inflate(R.layout.listagem_clientes, host.getTabContentView(), true);
		
		TabSpec abaSeqVisita = host.newTabSpec(ABA_SEQ_VISITAS);
		abaSeqVisita.setIndicator("", getResources().getDrawable(R.drawable.ic_seq_visita));
		abaSeqVisita.setContent(this);
		host.addTab(abaSeqVisita);

		TabSpec abaListaClientes = host.newTabSpec(ABA_LISTA_CLIENTES);
		abaListaClientes.setIndicator("", getResources().getDrawable(R.drawable.ic_listagem_completa));
		abaListaClientes.setContent(this);
		host.addTab(abaListaClientes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lista_clientes, menu);
		return true;
	}
	
	public void editaCliente(View v){
		Intent i = new Intent().setClass(this, ClienteActivity.class);
		FlowControl.getFlowControl(getIntent()).registry(i);
		startActivity(i);
	}

	public void finaliza(View v){
		Intent i = FlowControl.getNextActivity(getIntent(), getClass(), this);
		startActivity(i);
	}

	public View createTabContent(String tag) {
		
		try {
			return carregarSequenciaVisita();
		} catch (Exception e) {
			MessageUtil.showErrorMessage(this, e);
		}
		return null;
	}
	
	private View carregarSequenciaVisita() throws Exception{
		Collection<Cliente> clientes = carregarClientes("EXISTS SELECT 1 FROM", null);

		ListView list = new ListView(this);
		ListAdapter adapter = new ClienteListAdapter(this, android.R.layout.simple_list_item_1, clientes.toArray(new Cliente[clientes.size()]));
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> av, View v, int pos,	long id) {
				/*Cliente c = (Cliente) v.getTag(R.id.cadastroClientes);
				cadastroCliente(c._id);*/
			}
		});
		
		return list;
	}
	
	private Collection<Cliente> carregarClientes(String where, String [] params) throws Exception{
		DAO<Cliente> dao = new DAO<Cliente>(Cliente.class);
		Collection<Cliente> clientes = null;
		clientes = dao.list(where, params);
		return clientes;
	}
	
	public class ClienteListAdapter extends ArrayAdapter<Cliente>{
		
		Context context;
		int layoutResourceId;   
		Cliente data [] = null;
		
		public ClienteListAdapter(Context context, int layoutResourceId, Cliente[] data) {
			super(context, layoutResourceId, data);
			this.layoutResourceId = layoutResourceId;
			this.context = context;
			this.data = data;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView row = (TextView) convertView;
			
			if(row == null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				row = (TextView) inflater.inflate(layoutResourceId, parent, false);
			}
			
			Cliente itemVO = data[position];
			row.setText(itemVO.nome);
			//row.setTag(R.id.cadastroClientes, itemVO);
			
			return row;
		}
	}

}
